package com.reremouse.rerebbs.web.mb;

import com.reremouse.rerebbs.dao.IBbsUserDAO;
import com.reremouse.rerebbs.dao.IDictCityDAO;
import com.reremouse.rerebbs.dao.IDictProvinceDAO;
import com.reremouse.rerebbs.dao.IImgDAO;
import com.reremouse.rerebbs.model.BbsUser;
import com.reremouse.rerebbs.model.DictCity;
import com.reremouse.rerebbs.model.DictProvince;
import com.reremouse.service.ImageService;
import com.reremouse.util.JsfHelper;
import com.reremouse.util.SpringHelper;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@ManagedBean
@ViewScoped
public class UserDetail
        implements Serializable {
    BbsUser usr = null;
    String pwd_temp = "";
    String pwd_n1;
    String pwd_n2;
    String okScript;
    private IBbsUserDAO userDAO = (IBbsUserDAO) SpringHelper.getSpringBean("BbsUserDAO");
    private IImgDAO imgDAO = (IImgDAO) SpringHelper.getSpringBean("ImgDAO");
    List<SelectItem> provs = new ArrayList();
    List<SelectItem> citys = new ArrayList();
    IDictCityDAO cityDAO = (IDictCityDAO) SpringHelper.getSpringBean("DictCityDAO");
    IDictProvinceDAO provinceDAO = (IDictProvinceDAO) SpringHelper.getSpringBean("DictProvinceDAO");
    FileUpload fu = new FileUpload();
    int province = 10;
    int city = 1;

    public int getCity() {
        return this.city;
    }

    public void setCity(int city) {
        this.city = city;
    }

    public int getProvince() {
        return this.province;
    }

    public void setProvince(int province) {
        this.province = province;
    }

    public BbsUser getUsr() {
        return this.usr;
    }

    public void setUsr(BbsUser usr) {
        this.usr = usr;
    }

    public String getPwd_n1() {
        return this.pwd_n1;
    }

    public void setPwd_n1(String pwd_n1) {
        this.pwd_n1 = pwd_n1;
    }

    public String getPwd_n2() {
        return this.pwd_n2;
    }

    public void setPwd_n2(String pwd_n2) {
        this.pwd_n2 = pwd_n2;
    }

    public String getPwd_temp() {
        return this.pwd_temp;
    }

    public void setPwd_temp(String pwd_temp) {
        this.pwd_temp = pwd_temp;
    }

    public String getOkScript() {
        return this.okScript;
    }

    public void setOkScript(String okScript) {
        this.okScript = okScript;
    }

    public List<SelectItem> getCitys() {
        return this.citys;
    }

    public void setCitys(List<SelectItem> citys) {
        this.citys = citys;
    }

    public List<SelectItem> getProvs() {
        return this.provs;
    }

    public void setProvs(List<SelectItem> provs) {
        this.provs = provs;
    }

    @PostConstruct
    public void init() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String temp = request.getParameter("id");
        if (temp != null) {
            this.usr = this.userDAO.findBbsUser(Long.parseLong(temp.trim()));
        }
        if (this.usr != null) {
            DictProvince p = this.usr.getProvince();
            DictCity c = this.usr.getCity();
            if (p != null) {
                this.province = p.getNProvid().intValue();
            }
            if (c != null) {
                this.city = c.getNCityid().intValue();
            }
        }
        List<DictProvince> ps = this.provinceDAO.findAll(new int[]{0, 1000});
        for (DictProvince p : ps) {
            SelectItem s = new SelectItem(p.getNProvid(), p.getSProvname());
            this.provs.add(s);
        }
        List<DictCity> cs = this.cityDAO.findByNProvid(Integer.valueOf(this.province), new int[]{0, 1000});
        for (DictCity c : cs) {
            SelectItem s = new SelectItem(c.getNCityid(), c.getSCityname());
            this.citys.add(s);
        }
    }

    public void loadCities(AjaxBehaviorEvent e) {
        this.citys.clear();
        List<DictCity> cs = this.cityDAO.findByNProvid(Integer.valueOf(this.province), new int[]{0, 1000});
        for (DictCity c : cs) {
            SelectItem s = new SelectItem(c.getNCityid(), c.getSCityname());
            this.citys.add(s);
        }
    }

    public void up_action(FileUploadEvent event) {
        try {
            UploadedFile item = event.getFile();
            String str = "";
            Long tl = Long.valueOf(item.getSize());


            Float k = Float.valueOf(1024.0F);
            Float m = Float.valueOf(1048576.0F);
            Float g = Float.valueOf(1.073742E+009F);
            if ((float) tl.longValue() < k.floatValue()) {
                str = tl.toString() + "B";
            } else if ((k.floatValue() <= (float) tl.longValue()) && ((float) tl.longValue() < m.floatValue())) {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / k.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "KB";
            } else if (((float) tl.longValue() >= m.floatValue()) && ((float) tl.longValue() < g.floatValue())) {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / m.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "MB";
            } else {
                Float te = Float.valueOf(Math.round((float) tl.longValue() / g.floatValue() * 100.0F) / 100.0F);
                str = te.toString() + "GB";
            }
            String n = item.getFileName();
            int l1 = n.lastIndexOf("\\");
            int l2 = n.lastIndexOf(".");
            String ext = "." + n.substring(l2 + 1).toLowerCase();
            String name = n.substring(l1 + 1, l2);
            long imgId = System.nanoTime();
            this.imgDAO.saveImg(item.getInputstream(), imgId);
            if ((ext.toLowerCase().equals(".jpg")) || (ext.toLowerCase().equals(".gif")) || (ext.toLowerCase().equals(".png"))) {
                ImageService s = (ImageService) SpringHelper.getSpringBean("ImageService");
                s.transferUserImagesToRelativeDir((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(), Long.valueOf(imgId));
            }
            System.out.println("upload executed.");
            this.fu = new FileUpload();
            String picUrl = "servlet/ShowAbstractImage?id=" + imgId;
            this.usr.setPicUrl(picUrl);
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }

    public String saveUserChange() {
        boolean notOk = false;

        boolean hasPwd = true;
        if ((this.pwd_temp.equals("")) && (this.pwd_n1.equals("")) && (this.pwd_n2.equals(""))) {
            hasPwd = false;
        }
        FacesMessage fm = new FacesMessage();
        fm.setSeverity(FacesMessage.SEVERITY_ERROR);
        if ((this.pwd_temp.equals(this.usr.getPassword())) && (hasPwd)) {
            fm.setSummary("the old pass is error");

            FacesContext.getCurrentInstance().addMessage("", fm);
        } else if ((!this.pwd_n1.equals(this.pwd_n2)) && (hasPwd)) {
            fm.setSummary("两次输入的新密码不一致！");
            FacesContext.getCurrentInstance().addMessage("", fm);
        } else if ((this.pwd_n1.equals("")) && (hasPwd)) {
            fm.setSummary("the pass is not empty");

            FacesContext.getCurrentInstance().addMessage("", fm);
        } else {
            if (hasPwd) {
                this.usr.setPassword(this.pwd_n1);
            }
            this.usr.setProvince(this.provinceDAO.findById(Integer.valueOf(this.province)));
            this.usr.setCity(this.cityDAO.findById(Integer.valueOf(this.city)));
            this.userDAO.updateBbsUser(this.usr);
            ClientSession cs = (ClientSession) JsfHelper.getBean("clientSession");
            cs.setUsr(this.usr);
            fm.setSeverity(FacesMessage.SEVERITY_INFO);
            fm.setSummary("update success");

            FacesContext.getCurrentInstance().addMessage("", fm);
        }
        setOkScript("");
        return null;
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.UserDetail

 * JD-Core Version:    0.7.0.1

 */